package cn.wgx.module.ent.web;

import cn.wgx.common.base.IBaseController;
import cn.wgx.common.entity.ent.EntApp;
import cn.wgx.common.entity.sys.SysApp;
import cn.wgx.common.util.ResponseDataUtil;
import cn.wgx.module.ent.service.EntAppService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags = "5.1 企业应用管理")
@RestController
@RequestMapping("/api/entApp")
public class EntAppCtrl extends IBaseController<EntAppService, EntApp> {

    @ApiOperation(value = "根据实体参数查询列表,分页查询(page第几页, limit每页数量,默认20)")
    @GetMapping("list")
    public Map list(@ModelAttribute EntApp entApp,
                    @RequestParam(required = false, defaultValue = "1") Integer page,
                    @RequestParam(required = false, defaultValue = "20") Integer limit) {
        Page page1 = new Page(page, limit);
        return ResponseDataUtil.returnData(page1, service.getEntApps(page1, new QueryWrapper<>(entApp)));
    }

    @ApiOperation(value = "查询未授权的应用,分页查询(page第几页, limit每页数量,默认20)", notes = "必填：ent_id")
    @GetMapping("listAppNotInEnt")
    public Map listAppNotInEnt(@RequestParam Integer ent_id,
                               @RequestParam(required = false, defaultValue = "1") Integer page,
                               @RequestParam(required = false, defaultValue = "20") Integer limit) {
        Page<SysApp> page1 = new Page(page, limit);
        return ResponseDataUtil.returnData(page1, service.getAppNotInEnt(page1, ent_id));
    }

    @ApiOperation(value = "新增实体数据", notes = "必填：ent_id,app_id")
    @PostMapping("add")
    public Map add(@RequestBody EntApp t) {
        if (t.getApp_id() == null || t.getEnt_id() == null) {
            return ResponseDataUtil.returnErrorMap("缺少参数");
        }
        service.save(t);
        return ResponseDataUtil.returnData(t);
    }
}
